package com.tool4j.util.file;

import java.io.File;
import java.util.List;

/**
 * @author Deng.Weiping
 * @since 2023/8/7 9:38
 */
public class FileUtil {

    public static void main(String[] args) {
        List<File> fileNames = cn.hutool.core.io.FileUtil.loopFiles("E:\\code-template - 副本");
        for (File file : fileNames) {
            if (cn.hutool.core.io.FileUtil.isDirectory(file)) {
                continue;
            }
            String fileName = file.getName();
            if (fileName.contains("Controller")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/controller/_className_Controller.java.vm"), true);
            } else if (fileName.contains("ServiceImpl")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/service/impl/_className_ServiceImpl.java.vm"), true);
            } else if (fileName.contains("Service")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/service/_className_Service.java.vm"), true);
            } else if (fileName.contains("Entity.java.vm")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/entity/_className_.java.vm"), true);
            } else if (fileName.contains("Domain.java.vm")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/domain/_className_.java.vm"), true);
            } else if (fileName.contains("Mapper.java.vm")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/mapper/_className_Mapper.java.vm"), true);
            } else if (fileName.contains("Mapper.xml.vm")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/mapper/_className_Mapper.xml.vm"), true);
            } else if (fileName.contains("Domain.java.vm")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/domain/_className_.java.vm"), true);
            } else if (fileName.contains("Repository.java.vm")) {
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/repository/_className_Repository.java.vm"), true);
            } else if (fileName.contains("PageResult.java.vm")) {
                if (file.getPath().contains("jpa")) {
                    cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/domain/PageResult.java.vm"), true);
                } else {
                    cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/entity/PageResult.java.vm"), true);
                }
            }else if(fileName.contains("Specifications.java.vm")){
                cn.hutool.core.io.FileUtil.move(file, new File(file.getParentFile().getPath() + "/repository/_className_Specifications.java.vm"), true);
            }
        }

    }


    /**
     * 获取文件类型
     *
     * @param fileName
     * @return
     */
    public static String getFileType(String fileName) {
        int index = fileName.lastIndexOf(".");
        if (index < 0) {
            return "unknown";
        }
        return fileName.substring(index + 1);
    }

    /**
     * 获取文件大小(kb)
     *
     * @param len 文件长度
     * @return
     */
    public static String parserFileSize(Long len) {
        return String.format("%.2fkb", (double) len / 1024);
    }

    /**
     * 判断文件大小
     *
     * @param len  文件长度
     * @param size 限制大小
     * @param unit 限制单位（B,K,M,G）
     * @return
     */
    public static boolean checkFileSize(Long len, int size, String unit) {
        double fileSize = 0;
        if ("B".equalsIgnoreCase(unit)) {
            fileSize = (double) len;
        } else if ("K".equalsIgnoreCase(unit)) {
            fileSize = (double) len / 1024;
        } else if ("M".equalsIgnoreCase(unit)) {
            fileSize = (double) len / 1048576;
        } else if ("G".equalsIgnoreCase(unit)) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }
}
